package com.wgchao.identityrightsmanagement.domain.organization.model;

import com.wgchao.identityrightsmanagement.domain.IdentifiedDomainObject;
import com.wgchao.identityrightsmanagement.infrastructure.exception.ObjectDisabledException;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * @ClassName: Department
 * @author: wgchao
 * @createTime: 2020/6/6 10:25 AM
 * @Description: 部门
 */
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Department extends IdentifiedDomainObject {

    private String code;

    private String name;

    private String agencyCode;

    //描述/备注
    private String description;

    //禁用标志
    private boolean disabled;

    public Department(Long id, String code, String name, String agencyCode, String description, boolean disabled, Long version) {
        this.setId(id);
        this.code = code;
        this.name = name;
        this.agencyCode = agencyCode;
        this.description = description;
        this.disabled = disabled;
        this.setVersion(version);
    }

    public static Department addDepartment(Agency agency, String code, DepartmentCreator departmentCreator){
        if(!agency.normal()){
            throw new ObjectDisabledException("机构不可用");
        }
        return new Department(code, departmentCreator.getName(), agency.getAgencyCode(), departmentCreator.getDescription(), false);
    }

    /**
     * 修改部门的基本信息
     * @param departmentUpdator
     */
    public void modifyDepartmentInfo(DepartmentUpdator departmentUpdator, Agency agency){
        if(!agency.normal()){
            throw new ObjectDisabledException("机构不可用");
        }
        if(!normal()){
            throw new ObjectDisabledException("部门不可用");
        }
        if(departmentUpdator.getName() != null){
            this.name = departmentUpdator.getName().orElse(null);
        }
        if(departmentUpdator.getDescription() != null){
            this.description = departmentUpdator.getDescription().orElse(null);
        }
    }

    //状态是否正常
    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!this.normal()){
            throw new ObjectDisabledException(this.name+"部门不可用");
        }
    }

    //禁用
    public void disable(){
        this.disabled = true;
    }
    //恢复使用
    public void regain(){
        this.disabled = false;
    }

}
